package com.sas.web.panel.flows;

import java.util.Iterator; 
import java.util.List;

import org.apache.wicket.extensions.markup.html.repeater.data.sort.OrderByBorder;
import org.apache.wicket.extensions.markup.html.repeater.util.SortParam;
import org.apache.wicket.extensions.markup.html.repeater.util.SortableDataProvider;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.navigation.paging.PagingNavigator;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.model.IModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

import com.sas.domain.Flow;
import com.sas.model.EntityModel;
import com.sas.repository.FlowRepository;
import com.sas.web.panel.articles.ArticlesListPanel;
import com.sas.web.panel.vendors.VendorsShortListPanel;

@SuppressWarnings("serial")
public class FlowsShortListPanel extends Panel {

	@SpringBean
	private FlowRepository flowRpository;
	private ArticlesListPanel alp;
	private VendorsShortListPanel vlp;

	public FlowsShortListPanel(String id, ArticlesListPanel alp) {
		super(id);
		this.alp = alp;
		SortableFlowByArticleDataProvider<Flow> dp = new SortableFlowByArticleDataProvider<Flow>();
		dp.setSort("date", true);
		
		final DataView<Flow> dataView = createDataView(dp); 
		addOrderByBorders(dataView, dp);		
        
		add(dataView);
        add(new PagingNavigator("navigator", dataView));

	}

	public FlowsShortListPanel(String id, VendorsShortListPanel vlp) {
		super(id);
		this.vlp = vlp;
		
		SortableFlowByArticleFieldDataProvider<Flow> dp = new SortableFlowByArticleFieldDataProvider<Flow>();
		dp.setSort("date", true);
		
		final DataView<Flow> dataView = createDataView(dp); 
		addOrderByBorders(dataView, dp);		
        
		add(dataView);
        add(new PagingNavigator("navigator", dataView));
        
	}
	
	private void addOrderByBorders(final DataView<Flow> dataView, SortableDataProvider<Flow> dp) {
		add(new OrderByBorder("orderByDate", "date", dp){
			@Override
			protected void onSortChanged()
			{
				dataView.setCurrentPage(0);
			}
		});

		add(new OrderByBorder("orderByFlowType", "flowType", dp){
			@Override
			protected void onSortChanged()
			{
				dataView.setCurrentPage(0);
			}
		});

		add(new OrderByBorder("orderByName", "name", dp){
			@Override
			protected void onSortChanged()
			{
				dataView.setCurrentPage(0);
			}
		});		
	}

	private DataView<Flow> createDataView(SortableDataProvider<Flow> dp) {
		return new DataView<Flow>("flowsList", dp) {

			@Override
			protected void populateItem(final Item<Flow> item) {
				Flow v = item.getModelObject();
				item.add(new Label("date", v.getDate().toString()));
				item.add(new Label("flowType", v.getFlowType().name()));
				item.add(new Label("name", v.getSubject().getName()));	
				item.add(new Link<Void>("show") {

					@Override
					public void onClick() {
						System.out.println("aca voy al detalle de un flow");

					}
				});
			}

		};
	}

	
	private class SortableFlowByArticleDataProvider<Flow> extends SortableDataProvider<Flow>{ 
		
		@Override
		public Iterator<? extends Flow> iterator(int first, int count) {
			SortParam sp = getSort();
			List<Flow> ret = (List<Flow>) flowRpository.findByArticle(alp.getSelectedArticle(), sp.getProperty(), sp.isAscending()).subList(first, first + count);
			return ret == null ? null : ret.iterator();
		}

		@Override
		public IModel<Flow> model(Flow object) {
			return new EntityModel<Flow>(object.getClass(), object);
		}

		@SuppressWarnings("unchecked")
		@Override
		public int size() {
			SortParam sp = getSort();
			List<Flow> ret = (List<Flow>) flowRpository.findByArticle(alp.getSelectedArticle(), sp.getProperty(), sp.isAscending());
			return ret == null ? 0 : ret.size();
		}
	}
	
	private class SortableFlowByArticleFieldDataProvider<Flow> extends SortableDataProvider<Flow>{ 
		
		@Override
		public Iterator<? extends Flow> iterator(int first, int count) {
			SortParam sp = getSort();
			List<Flow> ret = (List<Flow>) flowRpository.findByArticleField(vlp.getSelectedVendor(), sp.getProperty(), sp.isAscending()).subList(first, first + count);
			return ret == null ? null : ret.iterator();
		}

		@Override
		public IModel<Flow> model(Flow object) {
			return new EntityModel<Flow>(object.getClass(), object);
		}

		@SuppressWarnings("unchecked")
		@Override
		public int size() {
			SortParam sp = getSort();
			List<Flow> ret = (List<Flow>) flowRpository.findByArticleField(vlp.getSelectedVendor(), sp.getProperty(), sp.isAscending());
			return ret == null ? 0 : ret.size();
		}
	}


}
